package com.lin.manager.notice.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lin.manager.common.ex.BizException;
import com.lin.manager.common.user.UserInfoContextHolder;
import com.lin.manager.notice.dao.NoticeUserDao;
import com.lin.manager.notice.model.entity.NoticeUserEntity;
import com.lin.manager.notice.service.NoticeUserService;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @作者: 林江
 * @创建时间: 2024/4/11
 * @功能:
 */
@Service
public class NoticeUserServiceImpl implements NoticeUserService {
    @Autowired
    NoticeUserDao noticeUserDao;

    @Override
    public void insertNoticeUser(Long noticeId, List<NoticeUserEntity> users) {
        if (CollectionUtil.isNotEmpty(users)) {
            users.forEach(item -> {
                item.setNoticeId(noticeId);
                noticeUserDao.insert(item);
            });
        } else {
            throw new BizException("请确认消息接收人");
        }
    }

    @Override
    public void deleteByNoticeId(List<Long> ids) {
        noticeUserDao.delete(new LambdaQueryWrapper<NoticeUserEntity>().in(NoticeUserEntity::getNoticeId, ids));
    }

    @Override
    public List<NoticeUserEntity> listByNoticeId(Long id) {
        LambdaQueryWrapper<NoticeUserEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NoticeUserEntity::getNoticeId, id);
        List<NoticeUserEntity> list = noticeUserDao.selectList(wrapper);
        return list;
    }

    @Override
    public void read(Long id) {
        LambdaUpdateWrapper<NoticeUserEntity> updateWrapper = new UpdateWrapper<NoticeUserEntity>().lambda()
                .eq(NoticeUserEntity::getNoticeId, id)
                .eq(NoticeUserEntity::getUserId, UserInfoContextHolder.getUserId())
                .set(NoticeUserEntity::getStatus, NumberUtils.INTEGER_ONE);

        noticeUserDao.update(null, updateWrapper);
    }

}
